<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2020-09-05 10:08:11
 * @LastEditors: Sam
 * @LastEditTime: 2020-12-16 17:03:37
 */
namespace app\admin\controller\system;
use app\admin\controller\AuthController;
use services\CacheService;
use think\facade\Db;
use think\facade\Request;
use services\Generate;
use think\facade\Env;
use services\JsonService as Json;

class Curd extends AuthController{
    private $modu;
    private $name;
    private $dir;
    private $namespaceSuffix;
    private $nameLower;
    private $data;

    /**
     * 自动生成页面
     *
     * @return void
     * @Description
     * @example
     * @author Sam
     * @since 
     */
    public function index(){
        $dbtables = Db::query('SHOW TABLE STATUS');
        $this->assign('tables', $dbtables);
        if(Request::param('table')){
            $table      = Request::param('table');
            $prefix     = Env::get(SERVER.".db_pre");
            $tableInfo  = Db::table($table)->getFieldsType();
            foreach ($tableInfo as $key => $value) {
                $tableInfo[] = $key;
                unset($tableInfo[$key]);
            }
            $controller = $this->parseName(preg_replace('/^(' . $prefix . ')/', '', $table), 1);
            $this->assign('table_info', json_encode($tableInfo));
            $this->assign('controller', $controller);
        }
        return $this->fetch();
    }

/**
 * 自动生成提交
 *
 * @return void
 * @Description
 * @example
 * @author Sam
 * @since 
 */
    public function run(){
        $generate = new Generate;
        $data     = Request::post();
        dump($generate->run($data, Request::post('file')));die;
         try{
            $generate->run($data, Request::post('file'));
         }catch(\Exception $e){
            Json::successful($e->getMessage());
         }
       
        Json::successful('创建成功');
    }

        /**
     * 字符串命名风格转换
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
     * @access public
     * @param  string  $name 字符串
     * @param  integer $type 转换类型
     * @param  bool    $ucfirst 首字母是否大写（驼峰规则）
     * @return string
     */
    public  function parseName($name, $type = 0, $ucfirst = true)
    {
        if ($type) {
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
                return strtoupper($match[1]);
            }, $name);
            return $ucfirst ? ucfirst($name) : lcfirst($name);
        }

        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
    }
}